6 Create Login Screen Application which shows Home screen if Login success otherwise displays error message using Android Studio.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 

xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 

android:layout_width="match_parent" 

android:layout_height="match_parent"

 android:orientation="vertical" 

tools:context=".MainActivity">

 

<EditText 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:id="@+id/edit1" 

android:hint="Enter User name" 

android:textSize="30sp" 

android:inputType="text"/>

 

<EditText 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:id="@+id/edit2" 

android:textSize="30sp" 

android:hint="Enter Password" 

android:inputType="textPassword"/>

<Button

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:id="@+id/button1" android:text="Login" android:layout_gravity="center" 

android:textSize="30sp" 

android:onClick="Login"/>

<TextView 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:id="@+id/text" 

android:layout_gravity="center" 

android:textSize="30sp"/>

</LinearLayout>


Layout Preview-1:


Activity_home_screen.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 

xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 

android:layout_width="match_parent" 

android:layout_height="match_parent" 

tools:context=".HomeScreen">


<TextView 

android:layout_width="match_parent" 

android:layout_height="match_parent" 

android:id="@+id/text" android:text="Welcome Dummy" 

android:textSize="30sp"/>


</LinearLayout>\


MainActivity.java:

package com.uday.android.a5a4_lc6; 

import android.content.Intent; 

import android.os.Bundle;

import android.view.View; 

import android.widget.Button; 

import android.widget.EditText; 

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

 public class MainActivity extends AppCompatActivity 

{

EditText e1,e2; 

Button b1; 

TextView t1; 

@Override

protected void onCreate(Bundle savedInstanceState) 

super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_main); 

e1=findViewById(R.id.edit1); 

e2=findViewById(R.id.edit2); 

t1=findViewById(R.id.text);

}

public void Login(View view) 

String s1= e1.getText().toString(); 

String s2= e2.getText().toString();

if(s1.equals("udaykumar")&&s2.equals("udaykumarthota")) 

Intent i = new Intent(this,HomeScreen.class); 

i.putExtra("key",s1);

startActivity(i);

}

else {

t1.setText("Invalid Credentials");

}

}

}

HomeScreen.java:

package com.uday.android.a5a4_lc6; 

import android.os.Bundle;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity; 

public class HomeScreen extends AppCompatActivity 

{

TextView tv1; 

@Override

protected void onCreate(Bundle savedInstanceState) 

super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_home_screen); 

tv1=findViewById(R.id.text);

String s =getIntent().getStringExtra("key"); 

tv1.setText("Welcome "+s);

}

}

Output:






 

Post a Comment

0 Comments